package com.cn.demo.service.impl;

import com.cn.demo.entity.*;
import com.cn.demo.mapper.*;
import com.cn.demo.service.CourseSelectionService;
import com.cn.demo.service.CurriculumService;
import com.cn.demo.service.OrganizationService;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private TeachingMapper teachingMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private InternshipLogMapper internshipLogMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;


    /**
     * 添加班级
     *
     * @param organization
     * @return
     */
    @Override
    public Result add(Organization organization) {
        if (curriculumMapper.getByCurriculumId(organization.getCurriculumId()) == null) {
            return Result.error("课程不存在");
        }
        organization.setOrganizationId(UUID.randomUUID().toString());
        if (organizationMapper.insert(organization) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 获取班级
     *
     * @param organization
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result get(Organization organization, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Organization> organizationList = organizationMapper.selectAll(organization);
        Map map = pageInfoUtilServer.currency(organizationList);
        return Result.success(map);
    }

    @Override
    public Result getOrganizationByCurriculumId(Integer page, Integer limit, Organization organization) {
        PageHelper.startPage(page, limit);
        List<Organization> organizationList = organizationMapper.getOrganizationByCurriculumId(organization);
        Map map = pageInfoUtilServer.currency(organizationList);
        return Result.success(map);
    }

    @Override
    public Result update(Organization organization) {
        if (organizationMapper.updateByPrimaryKey(organization) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result getByOrganizationId(String organizationId) {
        Organization organization = organizationMapper.getByOrganizationId(organizationId);
        if (organization != null) {
            return Result.success(organization);
        }
        return Result.error();
    }

    @Override
    public Result delete(String[] organizationIdList) {
        for (String organizationItem : organizationIdList) {
           Curriculum curriculumItem = curriculumMapper.getCurriculumByOrganizationId(organizationItem);
            //获得一个班级下面的所有学生
            List<Student>  studentList = teachingMapper.getOrganizationStudentByOrganizationId(organizationItem,null,null);
            for (Student studentItem : studentList) {
                //删除实验
                experimentMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem.getStudentId());
                evaluateMapper.delete(curriculumItem.getCurriculumExperimentId(),studentItem.getStudentId());
                //删除实训
                Internship internship = internshipMapper.getInternshipCorporation(studentItem.getStudentId(), curriculumItem.getCurriculumInternshipId());
                if (internship!=null){
                    internshipLogMapper.delete(internship.getInternshipId());
                    internshipMapper.deleteByPrimaryKey(internship.getInternshipId());
                }
                evaluateMapper.delete(curriculumItem.getCurriculumInternshipId(),studentItem.getStudentId());
                //删除课程设计
                Design designSubject = designMapper.getDesignSubject(studentItem.getStudentId(), curriculumItem.getCurriculumDesignId());
                if(designSubject!=null){
                    List<DesignTeam> designTeamList = designSubject.getDesignTeamList();
                    designTeamMapper.delete(designSubject.getDesignId(),studentItem.getStudentId());
                    evaluateMapper.delete(curriculumItem.getCurriculumDesignId(),studentItem.getStudentId());
                    if (designTeamList.size()==1){
                        //没有人直接删除设计
                        designMapper.deleteByPrimaryKey(designSubject.getDesignId());
                    }
                }
                List<String> StringListStudent = new ArrayList<>();
                StringListStudent.add(studentItem.getStudentId());
                //删除班级学生
                teachingMapper.organizationStudentDelete(StringListStudent,organizationItem);
            }
            //删除班级
            organizationMapper.deleteByPrimaryKey(organizationItem);
        }
        return Result.error();
    }

    @Override
    public Result division(String studentId, String organizationId) {
        return null;
    }

}